/**
 * 
 */
package com.effectiv.gooruda.business.impl;

import java.util.List;
import java.util.Locale;

import jodd.util.RandomStringUtil;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import org.springframework.security.authentication.encoding.PasswordEncoder;
import org.springframework.stereotype.Service;

import com.effectiv.gooruda.business.api.AbstractBusinessService;
import com.effectiv.gooruda.business.api.TenantCompanyUserService;
import com.effectiv.gooruda.domain.TenantCompany;
import com.effectiv.gooruda.domain.TenantCompanyUser;
import com.effectiv.gooruda.domain.Lov;

import com.effectiv.gooruda.repository.LovRepository;
import com.effectiv.gooruda.repository.TenantCompanyRespository;
import com.effectiv.gooruda.repository.TenantCompanyUserRespository;

/**
 * @author Dhrubo
 * 
 */
@Service
@Slf4j
public class TenantCompanyUserServiceImpl extends AbstractBusinessService<TenantCompanyUser, String>
		implements TenantCompanyUserService {

	@Autowired
	private TenantCompanyUserRespository repository;
	
	@Autowired
	private TenantCompanyRespository tenantCompanyRespository;

	@Autowired
	private LovRepository lovRepository;

	@Qualifier("passwordEncoder")
	@Autowired
	private PasswordEncoder encoder;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.effectiv.req1.business.api.UserService#findUsersByCompany(com.effectiv
	 * .req1.domain.Company)
	 */
	@Override
	public List<TenantCompanyUser> findUsersByCompany(TenantCompanyUser tenant) {

		if (tenant == null) {
			tenant = getTenantCompanyUser();
		}
		return null;
	}

	@Override
	public List<TenantCompanyUser> findByCompanyAndNameLike(TenantCompanyUser tenant, String name) {
		if (tenant == null) {
			tenant = getTenantCompanyUser();
		}
		return null;
	}

	@Override
	public void register(TenantCompanyUser user, Locale locale) {
		//check if company exists
		TenantCompany company = user.getTenantCompany();
		company = tenantCompanyRespository.findByName(company.getName());
		
		if(company == null){
			//save a new company
			company = tenantCompanyRespository.save(user.getTenantCompany());
			user.setTenantCompany(company);
		}
		
		String random = RandomStringUtil.randomAlpha(8);
		log.info("###### Password = {}", random);

		String pwd = encoder.encodePassword(random, null);

		user.setPassword(pwd);
		Lov role = lovRepository.findByName("ROLE_COMPANY_ADMIN");
		user.setRole(role);

		log.info("###### Registering user = {}", user);

		super.save(user);

		user.setPassword(random);
		
	}

	@Override
	public void save(TenantCompanyUser t) {

		String random = RandomStringUtil.randomAlpha(8);
		String pwd = encoder.encodePassword(random, null);

		t.setPassword(pwd);
		Lov role = lovRepository.findByName("ROLE_EMPLOYEE");
		t.setRole(role);
		t.setTenantCompany(getTenantCompany());
		
		super.save(t);

		t.setPassword(random);
		
	}

	@Override
	protected void initInternal() {
		setRepository(repository);
	}

	@Override
	public TenantCompanyUser activate(String id, Locale locale) {
		TenantCompanyUser user = repository.findOne(id);

		if (user != null) {

			user.setActive(true);
			super.update(user);

		}

		return user;
	}

}
